package hr.fer.zesoi.neumre;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.LayoutManager;

import hr.fer.zesoi.neumre.logic.TetrisBoard;
import hr.fer.zesoi.neumre.tetriminos.ITetrimino;
import hr.fer.zesoi.neumre.tetriminos.JTetrimino;
import hr.fer.zesoi.neumre.tetriminos.LTetrimino;
import hr.fer.zesoi.neumre.tetriminos.OTetrimino;
import hr.fer.zesoi.neumre.tetriminos.STetrimino;
import hr.fer.zesoi.neumre.tetriminos.TTetrimino;
import hr.fer.zesoi.neumre.tetriminos.Tetrimino;
import hr.fer.zesoi.neumre.tetriminos.ZTetrimino;
import hr.fer.zesoi.neumre.util.TableLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class TetrisVisualizer {

	private static Tetrimino[] basicTetriminos = { new OTetrimino(),
			new JTetrimino(), new LTetrimino(), new ZTetrimino(),
			new STetrimino(), new ITetrimino(), new TTetrimino() };
	private static Tetrimino[] workingTetriminos = { new OTetrimino(),
		new JTetrimino(), new LTetrimino(), new ZTetrimino(),
		new STetrimino(), new ITetrimino(), new TTetrimino() };

	private JFrame frame;
	private TetrisBoard board;
	private TetrisBirhtplaceComponent birthplace;
	private TetrisBoardComponent boardComp;
	private JLabel label;
	
	private int delay;

	public TetrisVisualizer(double width, double height, double rows, double cols, double delay)
			throws InterruptedException {
		
		this.delay = (int) delay;

		frame = new JFrame("TETRIS!");
		frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		
		double size[][] = {{TableLayout.FILL}, {0.20, 0.70, 0.1}};
		LayoutManager manager = new TableLayout(size);
		frame.setLayout(manager);
		
		
		frame.setSize((int) width, (int) height);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
		
		birthplace = new TetrisBirhtplaceComponent(4, (int) cols);
		frame.add(birthplace, "0, 0");

		board = new TetrisBoard((int) rows, (int) cols);
		boardComp = new TetrisBoardComponent(board);
		frame.add(boardComp, "0, 1");
		
		label = new JLabel("0");
		Font font = label.getFont();
		label.setFont(font.deriveFont(Font.BOLD, 50));
		label.setHorizontalAlignment(SwingConstants.CENTER);
		frame.add(label, "0, 2");
		
		//birthplace.setTetrimino(new TTetrimino());

//		Tetrimino tetrimino = new TTetrimino();
//
//		while (true) {
//
//			board.showTetrimino(tetrimino, 3, 6);
//			Thread.sleep(1000);
//			tetrimino.rotate();
//		}
	}

	/*public void showMove(int piece, int position, int rotation, int score) {

		Tetrimino tetrimino = tetriminos[piece];
		tetrimino.reset();
		
		for(int i = 1; i < rotation; i++)
			tetrimino.rotate();
		
		board.dropTetrimino(tetrimino, position);
		
		try {
			Thread.sleep(500);
		} catch (InterruptedException e) {}
		
		board.dropRows();
	}*/
	
	/*public void showMove(double[][] inBoard, double piece, double position, double rotation, double score) {
		
		int[][] board = new int[inBoard.length][inBoard[0].length];
		for(int row = 0; row < inBoard.length; row++)
			for(int col = 0; col < inBoard[0].length; col++)
				board[row][col] = (int) inBoard[row][col];
		
		this.board.setBoard(board);
		
		Tetrimino tetrimino = tetriminos[(int) piece];
		tetrimino.reset();
		
		for(int i = 1; i < rotation; i++)
			tetrimino.rotate();
		
		birthplace.setTetrimino(tetrimino);
		
		try {
			Thread.sleep(delay);
		} catch (InterruptedException e) {}
		
		this.board.dropTetrimino(tetrimino, (int) position);
		
		try {
			Thread.sleep(delay);
		} catch (InterruptedException e) {}
		
		this.board.dropRows();
		this.label.setText("" + (int) score);
	}*/
	
	public void showMove(double piece, double position, double rotation, double score) {
		
		Tetrimino tetrimino = workingTetriminos[(int) piece];
		tetrimino.reset();
		
		birthplace.setTetrimino(basicTetriminos[(int) piece]);
		
		for(int i = 1; i < rotation; i++)
			tetrimino.rotate();
		
		try {
			Thread.sleep(delay);
		} catch (InterruptedException e) {}
		
		this.board.dropTetrimino(tetrimino, (int) position);
		
		try {
			Thread.sleep(delay);
		} catch (InterruptedException e) {}
		
		this.board.dropRows();
		this.birthplace.setTetrimino(null);
		this.label.setText("" + (int) score);
		
		try {
			Thread.sleep(delay);
		} catch (InterruptedException e) {}
	}
	
	public void close() {
		
		this.frame.dispose();
	}
}
